import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_stort/config/routes.dart';
import 'package:flutter_stort/resource/colors.dart';
import 'package:flutter_stort/resource/string.dart';
import 'package:flutter_stort/utils/check_data_utils.dart';
import 'package:flutter_stort/utils/event_bus.dart';
import 'package:flutter_stort/utils/jump_util.dart';
import 'package:flutter_stort/utils/shared_preferences_utils.dart';
import 'package:flutter_stort/utils/toast_utils.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  TextEditingController _mobileController = TextEditingController();
  TextEditingController _passwordController = TextEditingController();
  bool _passwrodVisibility = false;
  bool _mobileVisibility = false;
  bool _isObscureText = true;
  IconData _iconVisibility = Icons.visibility;
  GlobalKey<FormState> _loginKey = GlobalKey();
  String username;
  String password;

  ///登录
  void _login() {
    _loginKey.currentState.save();
    if (checkData()) {
      SharedPreferencesUtils.saveInt("userId", 1);
      JumpPageUtils.clostPage(context);
      JumpPageUtils.startNext(context, RoutesUtils.MAIN_ROOT);
    }
  }

  ///数据校验
  bool checkData() {
    if (username.isEmpty) {
      ToastUtil.showToast("请输入用户名！");
      return false;
    }
    if (!CheckDataUtils.isPhone(username)) {
      ToastUtil.showToast("您输入的手机号码格式不正确");
      return false;
    }
    if (password.isEmpty) {
      ToastUtil.showToast("请输入密码！");
      return false;
    }
    if (password.length < 3 || password.length > 18) {
      ToastUtil.showToast("密码长度输入有误，长度在3-18位之间！");
      return false;
    }
    return true;
  }

  @override
  void initState() {
    EventBusUtils.eventBus.on<EventUserEntity>().listen((event) {
      setState(() {
        _mobileController.text = event.mobile;
        _passwordController.text = event.password;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: CustomColor.titleColor,
        title: Text(
          CustomString.LOGIN_TITLE,
          style: TextStyle(
            fontSize: 22,
            color: Colors.white,
          ),
        ),
        actions: [
          FlatButton(
            onPressed: () {
              JumpPageUtils.startNext(context, RoutesUtils.REGISTER_ROOT);
            },
            child: Text(
              "注册",
              style: TextStyle(
                color: Colors.white,
                fontSize: 18,
              ),
            ),
          ),
        ],
      ),
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.fromLTRB(10, 50, 10, 10),
          child: Form(
            key: _loginKey,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
                  decoration: BoxDecoration(
                    border: Border.all(
                      color: Colors.grey,
                      width: 0.5,
                      style: BorderStyle.solid,
                    ),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(Icons.phone_iphone_sharp),
                      SizedBox(
                        width: 4,
                      ),
                      Expanded(
                        flex: 1,
                        child: TextFormField(
                          controller: _mobileController,
                          keyboardType: TextInputType.phone,
                          textAlign: TextAlign.start,
                          maxLines: 1,
                          decoration: InputDecoration(
                            hintText: "请输入手机号码",
                            border: InputBorder.none,
                          ),
                          onChanged: (value) {
                            setState(() {
                              if (value.length > 0) {
                                _mobileVisibility = true;
                              } else {
                                _mobileVisibility = false;
                              }
                            });
                          },
                          onEditingComplete: () {},
                          onSaved: (value) {
                            username = value;
                          },
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          setState(() {
                            _mobileController.text = "";
                            _mobileVisibility = false;
                          });
                        },
                        child: Visibility(
                          visible: _mobileVisibility,
                          child: Container(
                            width: 40,
                            height: 40,
                            child: Icon(
                              Icons.close,
                              size: 20,
                              color: Colors.grey,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                SizedBox(
                  height: 20,
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
                  decoration: BoxDecoration(
                    border: Border.all(
                      color: Colors.grey,
                      width: 0.5,
                      style: BorderStyle.solid,
                    ),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(Icons.lock),
                      SizedBox(
                        width: 4,
                      ),
                      Expanded(
                        flex: 1,
                        child: TextFormField(
                          obscureText: _isObscureText,
                          controller: _passwordController,
                          keyboardType: TextInputType.visiblePassword,
                          textAlign: TextAlign.start,
                          maxLines: 1,
                          decoration: InputDecoration(
                            hintText: "请输入密码",
                            border: InputBorder.none,
                          ),
                          onChanged: (value) {
                            setState(() {
                              if (value.length > 0) {
                                _passwrodVisibility = true;
                              } else {
                                _passwrodVisibility = false;
                              }
                            });
                          },
                          onSaved: (value) {
                            password = value;
                          },
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          setState(() {
                            _passwordController.text = "";
                            _passwrodVisibility = false;
                          });
                        },
                        child: Visibility(
                          visible: _passwrodVisibility,
                          child: Container(
                            width: 40,
                            height: 40,
                            child: Icon(
                              Icons.close,
                              size: 20,
                              color: Colors.grey,
                            ),
                          ),
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          setState(() {
                            if (_isObscureText) {
                              _isObscureText = false;
                              _iconVisibility = Icons.visibility_off;
                            } else {
                              _isObscureText = true;
                              _iconVisibility = Icons.visibility;
                            }
                          });
                        },
                        child: Container(
                          width: 40,
                          child: Icon(
                            _iconVisibility,
                            color: Colors.grey,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                SizedBox(
                  height: 40,
                ),
                Container(
                  width: 400,
                  height: 50,
                  child: FlatButton(
                    shape: StadiumBorder(),
                    color: Colors.blue,
                    onPressed: _login,
                    child: Text(
                      CustomString.LOGIN_TITLE,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 20,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
